package com.example.questionnaire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;
import com.example.questionnaire.dao.AnswererTeamDao;
import com.example.questionnaire.entity.AnswererEntity;
import com.example.questionnaire.entity.AnswererTeamEntity;
import com.example.questionnaire.service.AnswererService;
import com.example.questionnaire.service.AnswererTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("answererTeamService")
public class AnswererTeamServiceImpl extends ServiceImpl<AnswererTeamDao, AnswererTeamEntity> implements AnswererTeamService {

    @Autowired
    AnswererTeamService answererTeamService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Integer pageNum = Integer.parseInt(params.get("page").toString());
        Integer limit = Integer.parseInt(params.get("limit").toString());
        Page<AnswererTeamEntity> page = new Page(pageNum, limit);
        this.page(page, new QueryWrapper<AnswererTeamEntity>());
        return new PageUtils(page);
    }

    @Autowired
    AnswererService answererService;

    @Override
    public PageUtils queryAnswerersInTeam(Map<String, Object> params) {
        QueryWrapper<AnswererTeamEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("team_id", params.get("teamId"));
        List<AnswererTeamEntity> list = this.list(queryWrapper);
        QueryWrapper<AnswererEntity> wrapper = new QueryWrapper<>();
        if(!list.isEmpty()){
            List<Integer> collect = list.stream().map(item -> {
                Integer answererId = item.getAnswererId();
                wrapper.or(item1 -> {
                    item1.eq("id", answererId).eq("is_deleted", 0);
                });
                return answererId;
            }).collect(Collectors.toList());
            IPage<AnswererEntity> page = answererService.page(
                    new Query<AnswererEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
        else {
            IPage<AnswererEntity> page = new Page<>();
            return new PageUtils(page);
        }

    }


}